package com.dragee.modules.officialWebsite.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.modules.officialWebsite.entity.PhotoAlbumDetailEntity;
import com.dragee.modules.officialWebsite.entity.PhotoAlbumEntity;
import com.dragee.modules.officialWebsite.service.PhotoAlbumDetailService;
import com.dragee.modules.officialWebsite.service.PhotoAlbumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;



/**
 * 相册
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-08-07 17:32:14
 */
@RestController
@RequestMapping("officialWebsite/photoalbum")
@Api(tags = "相册")
public class PhotoAlbumController {
    @Autowired
    private PhotoAlbumService photoAlbumService;
    @Autowired
    private PhotoAlbumDetailService photoAlbumDetailService;

    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("相册列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = photoAlbumService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("相册明细")
    public R info(@PathVariable("id") Long id){
		PhotoAlbumEntity photoAlbum = photoAlbumService.getById(id);
        List<PhotoAlbumDetailEntity> details = photoAlbumDetailService.getByPhotoId(photoAlbum.getId().toString());
        return R.ok().put("data", details);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改相册")
    public R saveOrUpdate(@RequestBody PhotoAlbumEntity photoAlbum){
        photoAlbumService.saveOrUpdate(photoAlbum);
        return R.ok();
    }

    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("相册删除")
    public R delete( String id){
		photoAlbumService.removeById(Long.parseLong(id));
        photoAlbumDetailService.remove(new QueryWrapper<PhotoAlbumDetailEntity>().eq("photoId", id));
        return R.ok();
    }

    /**
     * 保存
     */
    @PostMapping("pic/saveOrUpdate")
    @ApiOperation("保存或修改图片")
    public R picSaveOrUpdate(@RequestBody PhotoAlbumDetailEntity photoAlbumDetail){
        photoAlbumDetailService.saveOrUpdate(photoAlbumDetail);
        return R.ok();
    }

    /**
     * 删除
     */
    @GetMapping("pic/delete-by-id")
    @ApiOperation("删除图片")
    public R picDelete( String id){
        photoAlbumDetailService.removeById(Long.parseLong(id));
        return R.ok();
    }


}
